package com.neusoft.boot_ssm_crud.Controller;

import com.github.pagehelper.PageInfo;
import com.neusoft.boot_ssm_crud.domain.User;
import com.neusoft.boot_ssm_crud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/getUserPage/{pageNO}")
    public ModelAndView queryUserByPage(@PathVariable("pageNO") Integer pageNo)
    {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<User> pageInfo = userService.queryUserByPage(pageNo,5);
        modelAndView.addObject("page",pageInfo);
        modelAndView.setViewName("view/User_list");
        return modelAndView;
    }

    @RequestMapping("/to_add_User.do")
    public String to_add_User()
    {
        return "view/Add_user";
    }

    @RequestMapping("/add_User.do")
    public ModelAndView add_user(User user)
    {
        ModelAndView modelAndView = new ModelAndView();
        userService.insert(user);
        modelAndView.setViewName("redirect:/getUserPage/0");
        return modelAndView;
    }

    @RequestMapping("/to_modefy_User.do")
    public String to_modefy_User()
    {
        return "view/Add_user";
    }

    @RequestMapping("/update_User.do")
    public ModelAndView update_user(User user)
    {
        ModelAndView modelAndView = new ModelAndView();
        userService.updateByExample(user);
        modelAndView.setViewName("redirect:/getUserPage/0");
        return modelAndView;
    }

    @RequestMapping("/delete_User.do/{id}")
    public ModelAndView delete_user(@PathVariable("id") Long user_id)
    {
        ModelAndView modelAndView = new ModelAndView();
        userService.deleteByPrimaryKey(user_id);
        modelAndView.setViewName("redirect:/getUserPage/0");
        return modelAndView;
    }

    @RequestMapping("/query_UserByID.do")
    public ModelAndView queryUserByID(Long id)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",userService.selectByPrimaryKey(id));
        modelAndView.setViewName("view/Update_user");
        return modelAndView;
    }
}
